require "00_common"
require "date"


INPUT = "./http_payload/"
#OUTPUT = "./period/"

module TimePeriod
   def self.run
    puts ">> start to get http records..."

    begin
      all_files = TimePeriod.get_all_files
      puts all_files
      TimePeriod.handle_all_files all_files

      puts ">> end to get http records."
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end

  def self.get_all_files
    Common.get_all_files(INPUT)
  end


    def self.handle_each_file(file_name, global_start_time, global_end_time)

    puts "now handling : " + file_name
    file = File.open INPUT + file_name, "r"
    #output_file_name = Common.alternate_file_name(file_name, "_payload_", "TimePeriod")
    #output_file = File.open output_file_name, "w"

    begin
      counter = 0
      while line = file.readline
        counter = counter + 1

        line = line.rstrip
        if line.end_with? "|"
          line = line + "NULL"
        end

        fields = line.split "|"

        local_time_stamp    = Common.check_string fields[0]
        session_id          = Common.check_string fields[1]
        session_start_time  = Common.check_string fields[2]
        server_ip           = Common.check_string fields[3]
        server_port         = Common.check_string fields[4]
        client_ip           = Common.check_string fields[5]
        client_port         = Common.check_string fields[6]
        referer             = Common.check_string fields[7]
        host                = Common.check_string fields[8]
        content_type        = Common.check_string fields[9]
        content_length      = Common.check_string fields[10]
        user_agent          = Common.check_string fields[11]
        connection          = Common.check_string fields[12]
        c_request           = Common.check_string fields[13]
        s_response          = Common.check_string fields[14]
        cookie              = Common.check_string fields[15]


        if ((local_time_stamp <=> global_start_time) < 0)
          global_start_time = local_time_stamp
        end

        if ((local_time_stamp <=> global_end_time) > 0)
          global_end_time = local_time_stamp
        end

        if counter % 100000 == 0
          puts counter
        end
      end
    rescue Exception => e
      puts e.to_s
      file.close
    end

   puts "<start:#{global_start_time} == end:#{global_end_time}"
    period = Hash.new
    period["start"] = global_start_time
    period["end"] = global_end_time
    period
  end

  def self.handle_all_files(all_files)
    global_start_time = "12-31-2010 12:00"
    global_end_time = "12-01-2010 00:00"

    output_file = File.open "new_time_period.txt", "w"

    all_files.each do |file_name|
      period = handle_each_file file_name, global_start_time, global_end_time

      if ((period["start"] <=> global_start_time) < 0)
        global_start_time = period["start"]
      end

      if ((period["end"] <=> global_end_time) > 0)
        global_end_time = period["end"]
      end


    end
    output_file.puts "<start:#{global_start_time} == end:#{global_end_time}"



    output_file.close
  end
end
